package cn.getech.data.metadata.service.impl;

import cn.getech.data.metadata.entity.CollectionNotificationReceived;
import cn.getech.data.metadata.entity.CollectionNotificationUser;
import cn.getech.data.metadata.entity.meta.CollectionNotificationRelatednessUser;
import cn.getech.data.metadata.mapper.CollectionNotificationRelatednessUserMapper;
import cn.getech.data.metadata.service.CollectionNotificationReceivedService;
import cn.getech.data.metadata.service.CollectionNotificationUserGroupService;
import cn.getech.data.metadata.service.CollectionNotificationRelatednessUserService;
import cn.getech.system.center.utils.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service("collectionRelatednessNotificationUserService")
public class CollectionNotificationRelatednessUserServiceImpl extends ServiceImpl<CollectionNotificationRelatednessUserMapper, CollectionNotificationRelatednessUser> implements CollectionNotificationRelatednessUserService {

    @Autowired
    private CollectionNotificationRelatednessUserMapper collectionNotificationRelatednessUserMapper;

    @Autowired
    private CollectionNotificationReceivedService CollectionNotificationReceivedServiceImpl;

    @Autowired
    private CollectionNotificationUserGroupService collectionNotificationUserGroupService;

    /**
     * 根据用户组id查询需要发送的邮件人
     * @param notificationUserId
     * @return
     */
    @Override
    public List<CollectionNotificationReceived> queryByNotificationUserId(Long notificationUserId) {
        LambdaQueryWrapper<CollectionNotificationReceived> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CollectionNotificationReceived::getNotificationUserId,notificationUserId);
        return CollectionNotificationReceivedServiceImpl.list(queryWrapper);
    }

    /**
     * 查询免打扰时间
     * @param task    关联的id    比如 元数据id  或者质量管理id
     * @param relatednessType  类型  1为元数据   2为质量管理类型
     * @return
     */
    @Override
    public List<CollectionNotificationUser> queryNotificaCondition(Long task, String relatednessType) {
        LambdaQueryWrapper<CollectionNotificationRelatednessUser> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(CollectionNotificationRelatednessUser::getCollectionRelatednessId,task);
        lambdaQuery.eq(CollectionNotificationRelatednessUser::getRelatednessType,relatednessType);
        List<CollectionNotificationRelatednessUser> list = this.list(lambdaQuery);
        if(CollectionUtils.isNotEmpty(list)){
            //获取id集合
            List<Long> collect = list.stream().map(x -> x.getCollectionNotificationUserId()).collect(Collectors.toList());
            return  collectionNotificationUserGroupService.getByIds(collect);
        }
       return new ArrayList<>();
    }

    @Transactional
    @Override
    public void saveRelatednessNotification(CollectionNotificationRelatednessUser collectionNotificationRelatednessUser) {
        //先查询是否已经存在数据
        LambdaQueryWrapper<CollectionNotificationRelatednessUser> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CollectionNotificationRelatednessUser::getRelatednessType,collectionNotificationRelatednessUser.getRelatednessType());
        queryWrapper.eq(CollectionNotificationRelatednessUser::getCollectionNotificationUserId,collectionNotificationRelatednessUser.getCollectionNotificationUserId());
        queryWrapper.eq(CollectionNotificationRelatednessUser::getCollectionRelatednessId,collectionNotificationRelatednessUser.getCollectionRelatednessId());
        queryWrapper.last(" LIMIT 1");
        CollectionNotificationRelatednessUser collectionNotificationRelatedness = this.getOne(queryWrapper);
        //如果存在那么更新数据 否则保存数据
        if(collectionNotificationRelatedness!=null){
            collectionNotificationRelatednessUser.setId(collectionNotificationRelatedness.getId());
            collectionNotificationRelatednessUser.setUpdateUser(ShiroUtils.getUserId());
            collectionNotificationRelatednessUser.setUpdateTime(new Date());
            collectionNotificationRelatednessUser.setTenantId(ShiroUtils.getTenantId());
            this.updateById(collectionNotificationRelatednessUser);
        }else {
            collectionNotificationRelatednessUser.setCreateUser(ShiroUtils.getUserId());
            collectionNotificationRelatednessUser.setCreateTime(new Date());
            collectionNotificationRelatednessUser.setTenantId(ShiroUtils.getTenantId());
            this.save(collectionNotificationRelatednessUser);
        }
    }

    @Transactional
    @Override
    public void updateRelatednessNotification(List<CollectionNotificationRelatednessUser> collectionNotificationRelatednessUser,Long id,String type) {
        LambdaUpdateWrapper<CollectionNotificationRelatednessUser> updatePmcSalesArea = Wrappers.lambdaUpdate();
        updatePmcSalesArea.eq(CollectionNotificationRelatednessUser::getCollectionRelatednessId,id);
        updatePmcSalesArea.eq(CollectionNotificationRelatednessUser::getRelatednessType,type);
        collectionNotificationRelatednessUserMapper.delete(updatePmcSalesArea);
        //添加数据
        if(CollectionUtils.isNotEmpty(collectionNotificationRelatednessUser)){
            for(CollectionNotificationRelatednessUser notificationRelatednessUser:collectionNotificationRelatednessUser){
                this.saveRelatednessNotification(notificationRelatednessUser);
            }
        }
    }

    @Override
    public List<CollectionNotificationUser> queryCollectionNotificationRelatednessUserId(Long id, String typse) {
        LambdaQueryWrapper<CollectionNotificationRelatednessUser> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CollectionNotificationRelatednessUser::getRelatednessType,typse);
        queryWrapper.eq(CollectionNotificationRelatednessUser::getCollectionRelatednessId,id);
        List<CollectionNotificationRelatednessUser> list = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)){
            //查询所属用户组id列表
            List<Long> collect = list.stream().filter(Objects::nonNull).map(x -> x.getCollectionNotificationUserId()).collect(Collectors.toList());
            return  collectionNotificationUserGroupService.getByIds(collect);
        }
        return new ArrayList<>();
    }
}
